#!/bin/sh
set -e

PKGNAME="$1"
MODE="$2" # PRE-INSTALL, POST-INSTALL, DEINSTALL, POST-DEINSTALL

LOGFILE=/tmp/susan-setup.log

initialize_susan(){
		echo "$PKGNAME: initializing susan configuration, logs to ${LOGFILE}"
		/usr/lib/susan/scripts/susan-config initialize_local_hostname  >>${LOGFILE} 2>&1 || echo "initialize_local_hostname failed"
		/usr/lib/susan/scripts/susan-config initialize_passwords       >>${LOGFILE} 2>&1 || echo "initialize_passwords failed"
		/usr/lib/susan/scripts/susan-config initialize_database_driver >>${LOGFILE} 2>&1 || echo "initialize_database_driver failed"
}

case "$MODE" in
POST-INSTALL)
	case "$PKGNAME" in
		skyatlas.net-director*)
			initialize_susan
			#echo "enable the service with 'sysrc susandir_enable=YES'"
		;;

		skyatlas.net-filedaemon*)
			initialize_susan
			#echo "enable the service with 'sysrc susanfd_enable=YES'"

		;;

		skyatlas.net-storage*)
			initialize_susan
			#echo "enable the service with 'sysrc susansd_enable=YES'"
		;;
        esac
	;;
DEINSTALL)
	;;
PRE-INSTALL)
	true
	;;
POST-DEINSTALL)
	echo "deinstalling $PKGNAME"
	case "$PKGNAME" in
		skyatlas.net-director*)
			echo "shutting down susan-dir..."
			service susan-dir status && service susan-dir stop || echo " already stopped"
		;;

		skyatlas.net-filedaemon*)
			echo "shutting down susan-fd..."
			service susan-fd status && service susan-fd stop || echo " already stopped"
		;;

		skyatlas.net-storage*)
			echo "shutting down susan-fd..."
			service susan-sd status && service susan-sd stop || echo " already stopped"
		;;
        esac
	;;
esac
